package com.jia.leetCode;

public class Pro0122 {
    //求上升线段的和
    public int maxProfit2(int[] prices) {
        if(prices.length < 2) return 0;
        int result = 0;
        for(int i=1; i<prices.length; i++) {
        	if(prices[i-1] < prices[i])
        		result += (prices[i] - prices[i-1]);
        }
        return result;
    }
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int[][] dp = new int[prices.length][2];//dp[i][0]表示第i天不持有股票， dp[i][1]表示第i天持有股票
        //dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
        //dp[i][1] = max(dp[i-1][0] - prices[i], dp[i-1][1])
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for(int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);
            dp[i][1] = Math.max(dp[i-1][0] - prices[i], dp[i-1][1]);
        }
        return dp[prices.length - 1][0];
    }

    public static void main(String[] args) {
		int[] prices = {1,2,3,4,2,5};
		Pro0122 p = new Pro0122();
		System.out.println(p.maxProfit(prices));
	}
}
